﻿using System;
using System.Collections.Generic;
using System.Text;
using OABackground.Entities;
using OABackground.Infrastructure;

namespace OABackground.Services
{
    public class Material
    {
        public string goodsname;
        public int goodsnum;
        public string remarks;
        public string state;
    }
    public class MaterialApplySubmitService
    {
        public int AddMaterialApplyList(string departmentid, string teacherid, string goodsname, int goodsnum, float goodsprice, string remarks)
        {
            var repo = new MaterialapplyRepository();
            var MatericalApply = new Materialapply();
            MatericalApply.Departmentid = departmentid;
            MatericalApply.Tid = teacherid;
            MatericalApply.Goodsname = goodsname;
            MatericalApply.Goodsnum = goodsnum;
            MatericalApply.Goodsprice = goodsprice;
            MatericalApply.Remarks = remarks;
            //if (repo.Query(MatericalApply.ConventionState).ConventionState == 1)
            //{
            //    return false;
            //}
            //else

            return (AddMaterialApply(MatericalApply));
        }

        public int AddMaterialApply(Materialapply model)//添加新表项
        {
            int AA = -2;
            if (model.Goodsname == null)
                AA = -1;
            else if (model.Goodsnum <= 0)
                AA = 1;
            else if (model.Goodsprice == 0.0)
                AA = 2;
            else if (model.Remarks == null)
                AA = 3;
            else
            {
                using (var dbConnect = new oaContext())
                {
                    //Check here.

                    var repo = new MaterialapplySubmitRepository();
                    repo.Add(model);
                    AA = 0;

                }
            }

            return (AA);
        }

        /// <summary>
        /// 根据教师工号查询物资申请
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<Material> QueryMaterialByTid(string Id)
        {
            var matter = new MaterialapplySubmitRepository();
            List<Material> materialList = new List<Material>();
            var material = matter.QueryByTeacher(Id);
            for (int i = 0; i < material.Count; i++)
            {
                Material needMaterial = new Material
                {
                    goodsname = material[i].Goodsname,
                    goodsnum = material[i].Goodsnum,
                    remarks = material[i].Remarks
                };
                switch (material[i].State)
                {
                    case 0:
                        needMaterial.state = "未处理";
                        break;
                    case 1:
                        needMaterial.state = "同意";
                        break;
                    case 2:
                        needMaterial.state = "拒绝";
                        break;
                    default:
                        break;
                }
                materialList.Add(needMaterial);
            }
            return materialList;
        }
    }
}
